package servlet;


import entity.Project;
import service.Impl.ProjectServiceImpl;
import service.ProjectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

@WebServlet("/Project")
public class ProjectServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findAll_project";
        }
        switch (method) {
            // 查询所有项目信息
            case "findAll_project":
                ProjectService projectService = new ProjectServiceImpl();
                ArrayList<Project> projectList = null;
                projectList = projectService.getProjectList();
                req.setAttribute("projectList", projectList);
                req.getRequestDispatcher("jsp/project.jsp").forward(req, resp);
                break;
            // 接受添加请求，跳转到添加项目界面
            case "add":
                resp.sendRedirect("jsp/addProject.jsp");
                break;
            //添加
            case "addProject":
                Integer pid = Integer.valueOf(req.getParameter("projectId"));
                String pname = req.getParameter("projectName");
                String pcontent = req.getParameter("pcontent");
                Date ptime = Date.valueOf(req.getParameter("ptime"));
                Project project = new Project(pid,pname,pcontent,ptime);
                ProjectService projectService1 = new ProjectServiceImpl();
                //判断是否添加成功
                boolean isAdd = projectService1.addProject(project);
                // 如果成功添加，返回项目列表界面
                if (isAdd) {
                    resp.sendRedirect("Project");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 接受更新请求，跳转到更改界面
            case "update":
                Integer projectId = Integer.valueOf(req.getParameter("projectId"));
                ProjectService projectService2 = new ProjectServiceImpl();
                Project project1 = projectService2.findById(projectId);
                req.setAttribute("project", project1);
                req.getRequestDispatcher("jsp/updateProject.jsp").forward(req,resp);
                break;
            // 修改某个项目信息
            case "updateProject":
                // 获取项目信息
                pid = Integer.valueOf(req.getParameter("projectId"));
                pname = req.getParameter("projectName");
                pcontent = req.getParameter("pcontent");
                ptime = Date.valueOf(req.getParameter("ptime"));
                Project project2 = new Project(pid,pname,pcontent,ptime);
                ProjectService projectService3 = new ProjectServiceImpl();
                // 判断是否修改成功
                boolean isUpdate = projectService3.updateProject(project2);
                // 如果成功修改，返回项目列表界面
                if (isUpdate) {
                    resp.sendRedirect("Project");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 根据项目编号删除项目信息
            case "delete":
                Integer projectId1= Integer.valueOf(req.getParameter("projectId"));
                ProjectService projectService4 = new ProjectServiceImpl();
                boolean isDelete = projectService4.deleteProject(projectId1);
                if (isDelete) {
                    resp.sendRedirect("Project");
                } else {
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;


            default:
                break;
        }

    }

}
